<?php
namespace Admin\Controller;
use  Think\Controller;

class FlatController extends Controller{
    // 展示一共有多少名额
    public function index(){

        //实例化Memebr
        $num = M('status');
        // 导入分页类
        import('Org.Util.Page');
        $where['status'] = array('eq',1);
        $where['endTime'] = array('LT',time());

        $count = $num->where($where)->count();
        //查询满足满足条件的总记录条数
        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);
        //分页显示输出
        $show = $Page->show();
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $data = $num->where($where)->limit($limit)->order('starTime desc')->select();

        foreach($data as $k=>$v){

            $ids[] = $v['userId'];
        }
        $ids1 = implode(',',$ids);

        if($ids1){

            $res_user = M('user')->where('id in ('.$ids1.')')->select();
        }

        foreach($res_user as $k=>$v){

            if($v['level'] == 1){

                $list_id1[] = $v['detailid'];
            }

            if($v['level'] == 2){

                $list_id2[] = $v['detailid'];
            }

            if($v['level'] == 3){

                $list_id3[] = $v['detailid'];
            }

        }

        $list_id1 = implode(',',$list_id1);
        $list_id2 = implode(',',$list_id2);
        $list_id3 = implode(',',$list_id3);

        if($list_id1){
            $res_detail1 = M('detail1')->where('id in ('.$list_id1.')')->select();
        }

        if($list_id2){
            $res_detail2 = M('detail2')->where('id in ('.$list_id2.')')->select();
        }

        if($list_id3){

            $res_detail3 = M('detail3')->where('id in ('.$list_id3.')')->select();
        }


        foreach($data as $k=>$v){
            foreach($res_detail1 as $k1=>$v1){

                if($v['userid'] == $v1['userid']){
                    $data[$k]['phone'] = $v1['phone'];
                    $data[$k]['regtime'] = $v1['regtime'];
                }
            }

            foreach($res_detail2 as $k2=>$v2){

                if($v['userid'] == $v2['userid']){
                    $data[$k]['phone'] = $v2['phone'];
                    $data[$k]['regtime'] = $v2['regtime'];
                }
            }

            foreach($res_detail3 as $k4=>$v4){

                if($v['userid'] == $v4['userid']){
                    $data[$k]['phone'] = $v4['phone'];
                    $data[$k]['regtime'] = $v4['regtime'];
                }
            }

            foreach($res_user as $k3=>$v3){
                if($v['userid'] = $v3['id']){
                    $data[$k]['user'] = $v3['user'];
                    $data[$k]['sex'] = $v3['sex'];
                }
            }
        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['regtime'] = date('Y-m-d',$v['regtime']);
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function edit(){
        if(IS_AJAX){
            $uid = I('userid');
            $pid = I('pid');
            $level = I('level');

            $pid_num = M('cutnum')->where('userId = "'.$pid.'"')->field('sum(num) as number')->select();

            if($pid_num[0]['number'] <= 0){
                $this->ajaxReturn(['info'=>'增加失败','status'=>1,'url'=>U('Flat/index')]);
            }
            // 系统根据客户的要求 更新登记
            // 分配名额
            if($level == 2){

                $arr_num['num'] = -1;
                $arr_num['userId'] = $uid;
                $arr_num['geterId'] = $pid;

                $res_num = M('cutnum')->add($arr_num);

                $res_update = get_update($level,$pid,$uid);

                if($res_num && $res_update){

                    $this->ajaxReturn(['info'=>'增加成功','status'=>1,'url'=>U('Flat/index')]);
                }

            }else if($level == 3){

                $res_user = M('user')->where('id = "'.$uid.'"')->find();

                if($res_user['level'] != 1){
                    $data = getsystem();

                    if($res_user['level'] == 2){
                        $num = $data['firstnum']-1;
                    }else{
                        $num = $data['firstnum'];
                    }
                    if($pid_num[0]['number'] < $num){

                        $this->ajaxReturn(['info'=>'增加失败','status'=>1,'url'=>U('Flat/index')]);
                    }else{

                        $arr_num['num'] = $num;
                        $arr_num['userId'] = $uid;
                        $arr_num['geterId'] = $pid;

                        $arr_num1['num'] = -$num;
                        $arr_num1['userId'] = $pid;
                        $arr_num1['geterId'] = $uid;

                        $res_num1 = M('cutnum')->add($arr_num);
                        $res_num2 = M('cutnum')->add($arr_num1);

                        if($res_num1 && $res_num2){

                            $int = 1;
                        }else{
                            $int = 0;
                        }

                        $res_update = get_update($level,$pid,$uid);

                        if($int && $res_update){

                            $this->ajaxReturn(['info'=>'增加成功','status'=>1,'url'=>U('Flat/index')]);
                        }
                    }
                }else{

                    $data = getsystem();

                    $num = $data['firstnum'];

                    if($pid_num[0]['number'] < $num){

                        $this->ajaxReturn(['info'=>'增加失败','status'=>1,'url'=>U('Flat/index')]);
                    }else{

                        $arr_num['num'] = $num;
                        $arr_num['userId'] = $uid;
                        $arr_num['geterId'] = $pid;

                        $arr_num1['num'] = -$num;
                        $arr_num1['userId'] = $pid;
                        $arr_num1['geterId'] = $uid;

                        $arr_num2['num'] = -1;
                        $arr_num2['userId'] = $uid;
                        $arr_num2['geterId'] = $uid;

                        $res_num1 = M('cutnum')->add($arr_num);
                        $res_num2 = M('cutnum')->add($arr_num1);
                        $res_num3 = M('cutnum')->add($arr_num2);

                        if($res_num1 && $res_num2 && $res_num3){

                            $int = 1;
                        }else{
                            $int = 0;
                        }

                        $res_update = get_update($level,$pid,$uid);

                        if($int && $res_update){

                            $this->ajaxReturn(['info'=>'增加成功','status'=>1,'url'=>U('Flat/index')]);
                        }
                    }

                }
            }

        }else{

            $num = M('status');
            $where['status'] = array('eq',1);
            $where['endTime'] = array('LT',time());
            $where['userId'] = I('userid');
            $where['id'] = I('id');

            $data = $num->where($where)->find();


            $res_user = M('user')->where('id = "'.$where['userId'].'"')->find();

            $table = $res_user['level'];

            $res_detail1 = M($table)->where('id = "'.$res_user['detailid'].'"')->find();

            $data['phone'] = $res_detail1['phone'];
            $data['regtime'] = $res_detail1['regtime'];
            $data['user'] = $res_user['user'];
            $data['sex'] = $res_user['sex'];

            $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];
            

            $data['level'] = $getlevel[$data['level']];
            $data['regtime'] = date('Y-m-d',$data['regtime']);

            $this->assign('memberlist',$data);
            $this->display();
        }

    }
}
